import random
import string
import time

random.seed(1)
# 1.构造15个学生键值对{姓名：分数}, 存入学生字典
dct = {}
for i in string.ascii_uppercase[:15]:
    dct[i] = random.randint(35, 100)
print(dct)

# 2.按照学生成绩降序排列输出字典
a = sorted(dct.items(), key=lambda dct: dct[1], reverse=True)
print(a)

# 3.计算学生成绩平均分、最大值、最小值
scores = list(dct.values())
print("---", scores)
average = sum(scores) / len(scores)
c = max(scores)
d = min(scores)
print('平均分：{:^20.2f}'.format(average))
print('最大值：{:^20.2f}'.format(c))
print('最小值：{:^20.2f}'.format(d))

# 4.统计不及格人数的个数
num = 0
for i in scores:
    if i < 60:
        num = num + 1
print("不及格人数：{}".format(num))


# 5.输入学生名字，如W，输出学生对应的分数
def check_score():
    check_num = 0
    while True:
        try:
            name = (input("请输入要查询的学生姓名："))
            check_num += 1
            score = dct[name]
            average_score = sum(scores) / len(scores)
            if score > average_score:
                print("该生成绩属于上等水平")
            elif score == average_score:
                print("该生成绩属于中等水平")
            else:
                print("该生成绩属于下等水平")

            print("查询了{}次".format(check_num))
            break

        except KeyError:
            print('请输入正确的学生名字（A~O）')


# 6、查询时间、次数
t = time.time()
check_score()

print('耗时:{:.8f}s'.format(time.time() - t))
